// Copyright 2010 Alexandre Tristan St-Cyr <tristan.stcyr@gmail.com>

package com.codeplex.loadjava;


import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

class JavaLoaderOptions implements IJavaLoaderOptions {
	
	private String username;

	private String password;

	@SuppressWarnings("static-access")
	public static IJavaLoaderOptions create(String[] args) {
		
		Options options = new Options();
		options.addOption(OptionBuilder
			.hasArg(true)
			.withLongOpt("server")
			.withDescription("Name of the database to connect to. Default is 'localhost'")
			
			.create("s"));
		options.addOption(OptionBuilder
				.hasArg(true)
				.withLongOpt("database")
				.withDescription("Name of the database. Default is 'Repository'")
				.create("d"));
		options.addOption(OptionBuilder
				.isRequired()
				.hasArg(true)
				.withLongOpt("targetFolder")
				.withDescription("Repository folder in which to load")
				.create("tf"));
		options.addOption(OptionBuilder
				.isRequired()
				.hasArg(true)
				.withLongOpt("username")
				.withDescription("Username to connect to the database")
				.create("u"));
		options.addOption(OptionBuilder
				.isRequired()
				.hasArg(true)
				.withLongOpt("password")
				.withDescription("Password to connect to the database")
				.create("p"));

		try {
			CommandLineParser parser = new GnuParser();
			CommandLine line = parser.parse(options, args);
			
			JavaLoaderOptions javaLoaderOptions = new JavaLoaderOptions();
			javaLoaderOptions.username = line.getOptionValue("username");
			javaLoaderOptions.password = line.getOptionValue("password");
			javaLoaderOptions.targetFolder = line.getOptionValue("targetFolder");
			String database = line.getOptionValue("database");
			if (database != null) {
				javaLoaderOptions.databaseName = database;
			}
			String server = line.getOptionValue("server");
			if (server != null)
				javaLoaderOptions.server = server;
				
			String[] remainingArgs = line.getArgs();
			
			if (remainingArgs.length < 1) {
				printHelp(options);
				return null;
			}
			
			javaLoaderOptions.classpath = remainingArgs[0];
			return javaLoaderOptions;
		} catch (ParseException e) {
			System.err.println(e.getMessage() );
			printHelp(options);
			return null;
		}
	}

	private static void printHelp(Options options) {
		HelpFormatter formatter = new HelpFormatter();
		formatter.printHelp("loadjava classpath", options);
	}
	
	private String databaseName = "Repository";
	
	private String targetFolder;
	
	private String classpath;
	
	private String server = "127.0.0.1:1433";
	
	private JavaLoaderOptions() {}
	
	@Override
	public String getDatabaseName() {
		return this.databaseName;
	}
	
	@Override
	public String getTargetFolder() {
		return this.targetFolder;
	}

	@Override
	public String getClasspath() {
		return this.classpath;
	}
	
	@Override
	public String getServer() {
		return this.server;
	}
	
	@Override
	public String getUsername()
	{
		return this.username;
	}
	
	@Override
	public String getPassword()
	{
		return this.password;
	}
}
